package com.purchase.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.purchase.entity.Coupon;
import com.purchase.entity.WxUserMember;
import com.purchase.mapper.MemberOrderMapper;
import com.purchase.service.MemberOrderService;
import com.purchase.service.MemberPayService;
import com.purchase.util.LogInfo;
import com.purchase.util.OrderIdGenerater;
import com.purchase.util.RequestForm;
import com.purchase.util.ResponseForm;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MemberOrderServiceImpl implements MemberOrderService {

    @Autowired
    private MemberOrderMapper memberOrderMapper;

    @Autowired
    private MemberPayService memberPayService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseForm createOrder(RequestForm param) {
        ResponseForm result = new ResponseForm();
        Map<String, Object> paramMap = (Map<String, Object>) param.getData();
        String wxUid = null;
        try {
            wxUid = (String) paramMap.get("wxUid");
            String price = (String) paramMap.get("price");
            String roleId = (String) paramMap.get("roleId");
            String invitePhone = (String) paramMap.get("invitePhone");
            if (!StringUtils.isNotBlank(wxUid)
                    || !StringUtils.isNotBlank(price)
                    || !StringUtils.isNotBlank(roleId)) {
                result.setStatus(false);
                result.setMessage(LogInfo.PARAM_ERROR);
                log.error(LogInfo.PARAM_ERROR);
                return result;
            }

            //校验是否已经是会员
            WxUserMember memberByWxUid = memberOrderMapper.getMemberByWxUid(paramMap);
            if (memberByWxUid != null && memberByWxUid.getStatus() == 1 && memberByWxUid.getRoleId() != 1) {
                //如果是会员，在有效期内，则不需支付
                result.setStatus(false);
                result.setMessage("已是会员");
                return result;
            }

            //生成订单号
            String orderId = OrderIdGenerater.generateMemberOrderId();
            //组装订单
            HashMap<String, String> orderMap = new HashMap<>();
            orderMap.put("id", orderId);
            orderMap.put("wxUid", wxUid);
            orderMap.put("price", price);
            orderMap.put("roleId", roleId);
            orderMap.put("invitePhone", invitePhone);

            //订单和详情入库
            memberOrderMapper.createMemberOrder(orderMap);
            log.info("购买会员订单入库,订单号={}", orderId);

            Map<String, String> payResult = memberPayService.doUnifiedOrder(orderMap);
            result.setData(payResult);

        } catch (Exception e) {
            result.setStatus(false);
            result.setMessage(LogInfo.ERROR);
            log.error("购买会员订单入库error,wxUid=" + wxUid, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }

    @Override
    public ResponseForm getOrderListByStatus(RequestForm param) {
        ResponseForm result = new ResponseForm();
        Map<String, Object> paramMap = (Map<String, Object>) param.getData();
        try {
            //获取订单列表
            com.purchase.util.Page.parsePage(paramMap);
            Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
            List<Map<String, Object>> list = memberOrderMapper.getOrderByStatus(paramMap);
            result.setData(list);
            result.setTotal((int) pageHelper.getTotal());
        } catch (Exception e) {
            result.setStatus(false);
            result.setMessage(LogInfo.ERROR);
            log.error("获取订单列表error", e);
        }
        return result;
    }

}
